<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php学习笔记[032]</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="show"><span>032 cookie和session</span></div>
<?php include 'include/top.php';?>

<div class="text">
<b>cookie</b>
使用方法<strong>setcookie('名称','值','过期时间','路径','作用域','传输模式')</strong><br />
我们来看下面的代码<br />
<div class="code">
  &lt;?php<br />
    if(isset($_GET['act']))<br />
    {<br />
    setcookie(&quot;user&quot;,'');<br />
    setcookie(&quot;user&quot;,''); <br />
    echo &quot;&lt;script&gt;location.href='study032.php'&lt;/script&gt;&quot;; <br />
    }<br />
    if (@$_POST['user']&amp;&amp;$_POST['pass'])<br />
    {<br />
    setcookie(&quot;user&quot;,$_POST['user'],time()+3600);<br />
    setcookie(&quot;pass&quot;,$_POST['pass'],time()+3600);<br />
    echo &quot;&lt;script&gt;location.href='study032.php'&lt;/script&gt;&quot;; <br />
    }
  if (@$_COOKIE['user']&amp;&amp;$_COOKIE['pass'])<br />
    {<br />
    echo &quot;用户名:&quot;.$_COOKIE['user'].&quot;&lt;br /&gt;密码：&quot;.$_COOKIE['pass'].&quot;&lt;br /&gt;&quot;;<br />
    echo &quot;&lt;a href='study032.php?act=del'&gt;退出&lt;/a&gt;&quot;;<br />
    }<br />
    ?&gt;<br />
  &lt;form action=&quot;study032.php&quot; method=&quot;post&quot;&gt;<br />
  &lt;input name=&quot;user&quot;&gt;&lt;br /&gt;<br />
  &lt;input name=&quot;pass&quot; type=&quot;password&quot;&gt;&lt;br /&gt;<br />
  &lt;input type=&quot;submit&quot; value=&quot;提交&quot;&gt;<br />
  &lt;/form&gt;<br />
</div>
我们运行它来看效果：
<div class="effect">
<?php
if(isset($_GET['act']))
	{
	setcookie("user",'');
	setcookie("pass",'');	
	echo "<script>location.href='study032.php'</script>";	
	}
if (@$_POST['user']&&$_POST['pass'])
	{
	setcookie("user",$_POST['user'],time()+3600);
	setcookie("pass",$_POST['pass'],time()+3600);
	echo "<script>location.href='study032.php'</script>";	
	}

if (@$_COOKIE['user']&&$_COOKIE['pass'])
	{
	echo "用户名:".$_COOKIE['user']."<br />密码：".$_COOKIE['pass']."<br />";
	echo "<a href='study032.php?act=del'>退出</a>";
	}
?>
<form name="form1" action="study032.php" method="post">
<input name="user"><br />
<input name="pass" type="password"><br />
<input type="submit" value="提交">
</form>
</div>
</div>

<div class="text">
<b>session</b>
<strong>session</strong>使用的时候其启动函数必须放在页面的首要位置，不允许有任何的html代码或程序代码，注释可以存在。<br />
我们来看一下session用到的函数：<br />
<strong>session_start();</strong>                    //初始化session.需在文件头部<br />
<strong>$_SESSION[name]=value;</strong>  //配置Seeeion<br />
<strong>echo $_SESSION[name];</strong>    //使用session<br />
<strong>isset($_SESSION[name]);</strong>   // 判断<br />
<strong>unset($_SESSION[name]);</strong>   //删除<br />
<strong>session_destroy()；</strong>             //消耗所有session<br />
<strong>session</strong>使用起来比<strong>cookie</strong>简单许多，我们仅用代码来说明它<br />
<div class="code">&lt;?php session_start();?&gt;<br />
&lt;?php<br />
if(@$_POST['name1']&amp;&amp;$_POST['pass1'])<br />
{<br />
$_SESSION['name1']=$_POST['name1'];<br />
$_SESSION['pass1']=$_POST['pass1'];<br />
}<br />
if(isset($_GET['action']))<br />
{<br />
unset($_SESSION['name1']);<br />
unset($_SESSION['pass1']);<br />
}<br />
if(isset($_SESSION['name1'])&amp;&amp;isset($_SESSION['pass1']))<br />
{<br />
echo 'name1:'.$_SESSION['name1'].'&lt;br /&gt;pass1:'.$_SESSION['pass1'].'&lt;br /&gt;';<br />
echo &quot;&lt;a href='study032.php?action=del'&gt;退出&lt;/a&gt;&quot;;<br />
}<br />
?&gt;<br />
&lt;form name=&quot;form2&quot; action=&quot;study032.php&quot; method=&quot;post&quot;&gt;<br />
&lt;input name=&quot;name1&quot;&gt;&lt;br /&gt;<br />
&lt;input name=&quot;pass1&quot; type=&quot;password&quot;&gt;&lt;br /&gt;<br />
&lt;input type=&quot;submit&quot; value=&quot;提交&quot;&gt;<br />
&lt;/form&gt;<br />
</div>
我们来看运行效果:
<div class="effect">
<?php
if(@$_POST['name1']&&$_POST['pass1'])
	{
	$_SESSION['name1']=$_POST['name1'];
	$_SESSION['pass1']=$_POST['pass1'];
	}
if(isset($_GET['action']))
	{
	unset($_SESSION['name1']);
	unset($_SESSION['pass1']);
	}
if(isset($_SESSION['name1'])&&isset($_SESSION['pass1']))
	{
	echo 'name1:'.$_SESSION['name1'].'<br />pass1:'.$_SESSION['pass1'].'<br />';
	echo "<a href='study032.php?action=del'>退出</a>";
	}
?>
<form name="form2" action="study032.php" method="post">
<input name="name1"><br />
<input name="pass1" type="password"><br />
<input type="submit" value="提交">
</form>
</div>
</div>
</body>
</html>
